﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.Exceptions;
using SCRUM.Core.Exceptions.Database;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Widget for managing user settings.
/// </summary>
public partial class UserSettings : WidgetBase
{
    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get { return "UserSettings"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return "User settings"; }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return false; }
    }

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.UserDetailsEdit.PasswordChangeSuccessEvent += new EventHandler(PasswordChangedSuccess);
        this.UserDetailsEdit.PasswordChangeFailEvent += new EventHandler(PasswordChangedFail);
    }

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        this.BindThemes();
        SCRUM.Core.BusinessObjects.UserSettings us = DAOUserSettings.GetSettingsForLoggedUser();
        this.DropDownListThemes.SelectedValue = us.Theme;
        User u = DAOUser.GetLoggedUser();
        UserDetailsEdit.SelectedItem = u;
        loggedUser = u;
    }

    /// <summary>
    /// Binds the themes.
    /// </summary>
    protected void BindThemes()
    {
        this.DropDownListThemes.DataSource = SCRUM.Core.Configuration.ConfigManager.CurrentConfig.Themes;
        this.DropDownListThemes.DataBind();
    }

    /// <summary>
    /// Saves the settings.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SaveSettings(object sender, EventArgs e)
    {
        try
        {
            SCRUM.Core.BusinessObjects.UserSettings us = new SCRUM.Core.BusinessObjects.UserSettings(
            0,
            this.LanguageBar1.GetSelectedLanguage(),
            this.DropDownListThemes.SelectedValue);

            DAOUserSettings.UpdateSettingsForLoggedUser(us);
            Response.Redirect(this.Page.Request.Url.PathAndQuery);
        }
        catch (SCRUMException ex)
        {
            this.ShowOperationResult(MessageType.Error, ex.Message);
        }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Raises when user wants to Save their changes
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonSaveItem_Click(object sender, EventArgs e)
    {
        UserDetailsEdit.EditItem(UserDetailsEdit.SelectedItem.ID);
        //this.UserDetails.Visible = false;
        //HideButtons();
    }

    /// <summary>
    /// Raises when user wants to cancel editting User
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonCancelItem_Click(object sender, EventArgs e)
    {
        UserDetailsEdit.SelectedItem = loggedUser;
        //HideButtons();
        //this.UserDetails.Visible = false;
    }

    /// <summary>
    /// Passwords changed successfully.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void PasswordChangedSuccess(object sender, EventArgs e)
    {
        this.ShowOperationResult(MessageType.Info, "PasswordChanged");
    }

    /// <summary>
    /// Passwords change failed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void PasswordChangedFail(object sender, EventArgs e)
    {
        this.ShowOperationResult(MessageType.Error, "PasswordNotChanged");
    }

    /// <summary>
    /// User that is currently logged in
    /// </summary>
    private User loggedUser;
}
